var app = getApp()
var utils = require('../../utils/utils.js')
Page( {
  data: {
    showTipWords:false,
    cartIcon:utils.sysConst.cartIcon,
    addIcon:utils.sysConst.addIcon,
    removeIcon:utils.sysConst.removeIcon,
    tipWords: '购物车空空如也'
  },
   //初始化购物车信息
  onLoad: function() {
    wx.showLoading({
      title: '加载中...',
    })
     console.log("==cart-init==")
    var that = this
    //=== start ===
    wx.login({
      success: function (res) {
        var param = {"code":res.code,"appId":app.globalData.appId}
        wx.request({
          url: utils.sysConst.getUserInfoPath,
          method: 'POST',
          header: {
            "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
          },
          data: param,
          dataType: "json",
          success: function(res) {
            if (res.data.code == 1){
              //设置全局用户信息-openid
              app.globalData.openid = res.data.data.openid
              //console.log("app.globalData.openid-cart",app.globalData.openid)
              //获取购物车信息-start
              var paramCart = {"openId":res.data.data.openid};
              wx.request({
                url: utils.sysConst.getCartPath,
                method: 'POST',
                header: {
                  "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(paramCart)
                },
                data: paramCart ,
                dataType: "json",
                success: function(res) {
                  if (res.data.code == 1){
                    that.setData({
                      showTipWords:false,
                      cartProduct :res.data.data.productInfoList,
                      discountAmount: res.data.data.discountAmount,
                      orderAmount: res.data.data.orderAmount,
                      canOrder: res.data.data.canOrder,
                      errorMsg: res.data.data.errorMsg
                    })
                  }else{
                    that.setData({
                      cartProduct :null,
                      canOrder: false,
                      showTipWords:true,
                    })
                  }
                }
              })
              //获取购物车信息-end
            }else{
              wx.showToast({
                title:"获取用户信息失败："+res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    });
    wx.hideLoading();
    //=== end ===
  },
  //重新打开
  onShow(){
    this.onLoad()
  },
  //加入购物车-start
  addChartFromCart:function(event){
    wx.showLoading({
      title: '处理中...',
    })
    var that = this
    var param= {"productId":event.currentTarget.dataset.id,"openId":app.globalData.openid}
    wx.request({
      url: utils.sysConst.addCartPath,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
      },
      data: param,
      dataType: "json",
      success: function(res) {
        // console.log("addCart-back====",res.data)
        wx.hideLoading();
        if (res.data.code == 1){
          wx.showToast({
            title: '添加成功',
            icon: 'success',
            duration: 2000
          })
        }else{
          wx.showToast({
            title:"添加失败："+res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
    this.onLoad();
  },
  //从购物车删除-start
  removeChartFromCart:function(event){
    wx.showLoading({
      title: '处理中...',
    })
    var that = this
    var param = {"productId":event.currentTarget.dataset.id,"openId":app.globalData.openid}
    wx.request({
      url: utils.sysConst.removeCartPath,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
      },
      data: param,
      dataType: "json",
      success: function(res) {
        wx.hideLoading();
        if (res.data.code == 1){
          wx.showToast({
            title: '删除成功',
            icon: 'success',
            duration: 2000
          })
        }else{
          wx.showToast({
            title:"删除失败："+res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
    this.onLoad();
  },
  // 下单
  goOrder:function(event){
    wx.navigateTo({
      url: '/pages/order/order?id=cart',
    })
  },
  // === end ===
})
